package com.uplift.mobile.user.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uplift.common.Const;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper;
import com.uplift.mobile.user.model.User;
import com.uplift.mobile.user.model.UserInfo;
import com.uplift.mobile.user.service.IUserService;

/**
 * 用户信息相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * 注册
	 * 
	 * @param request
	 * @param userInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/register")
	@ResponseBody
	public IResultable register(HttpServletRequest request, UserInfo userInfo) {
		try {
			int ret = 0;
			if (StringUtils.isBlank(userInfo.getUsername())) {
				ret = -3;
			} else if (StringUtils.isBlank(userInfo.getPassword())) {
				ret = -3;
			} else if (StringUtils.isBlank(userInfo.getEmail())) {
				ret = -3;
			} else if (StringUtils.isBlank(userInfo.getSex())) {
				ret = -3;
			}
			if(ret == 0){
				if (StringUtils.isBlank(userInfo.getUserCode())) {
					userInfo.setUserCode(userInfo.getSourceCode() + System.currentTimeMillis());
				}
				if (StringUtils.isBlank(userInfo.getBirthday())) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					userInfo.setBirthday(sdf.format(new Date()));
				}
				ret = this.userService.register(userInfo);
			}
			if (ret > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "注册成功");
			} else if (ret == -1) {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "注册失败，已关闭注册");
			} else if (ret == -2) {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "注册失败，用户已存在");
			} else if (ret == -3) {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "注册失败，缺少必要字段");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "注册失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "注册失败");
		}
	}

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public IResultable login(HttpServletRequest request, User user) {
		// 登录状态
		int ret = this.userService.login(request, user);
		// 根据状态返回登录信息
		if (ret == 1) {
			Map<?, ?> userMap = (Map<?, ?>) request.getAttribute(Const.REQUEST_USER);
			userMap.remove("password");
			return ResultWrapper.wrapMap(userMap, ResultWrapper.SUCC, "登录成功");
		} else if (ret == 2) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "登录失败，账号或密码不能为空");
		} else if (ret == 3) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "登录失败，账号被禁用");
		} else {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "登录失败，账号或密码错误");
		}
	}

	/**
	 * 登出
	 * 
	 * @param request
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/logout")
	@ResponseBody
	public IResultable logout(HttpServletRequest request, User user) {
		try {

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "注销成功");
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param user
	 * @return IResultable
	 */
	@RequestMapping(value = "/modifyPwd")
	@ResponseBody
	public IResultable modifyPwd(HttpServletRequest request, User user) {
		try {
			int code = this.userService.modifyPwd(user);
			if (code == 1) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "修改成功");
			} else if (code == 2) {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "修改失败，原密码不正确");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "修改失败");
		}
	}

	@RequestMapping(value = "/findPwd")
	@ResponseBody
	public IResultable findPwd(HttpServletRequest request, User user) {
		try {
			int code = this.userService.findPwd(user);
			if (code == 1) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "密码已发送到您的邮箱，请注意查收并修改密码");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "密码找回失败，请检查邮箱是否正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "密码找回失败");
		}
	}
	
	@RequestMapping(value = "/modifyEmail")
	@ResponseBody
	public IResultable modifyEmail(HttpServletRequest request, User user) {
		try {
			int ret = this.userService.modifyUser(user);
			if (ret > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "邮箱修改成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "邮箱修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "邮箱修改失败");
		}
	}	
}
